<?php
require_once 'MyPdoCounty.class.php';
class TableShopStore extends MyPdoCounty{
	private $tableName = 'shop_store';


	//更改商户信息
	public function _updateStoreInfo(&$tmp_arr){
		$account = I('user','p');
		$name = I('name','p');
		$phone = I('phone','p');
		$qq = I('qq','p');
		$wx = I('wx','p');
		$addr = I('addr','p');
		$info = I('info','p');
		$banner = '';

		$sqlc = "select change_count,change_time from {$this->tableName} where account = ?";
		$resc = $this->dql($sqlc,array($account),1);
		if($resc['change_count'] >= $this->config['storeChangeCount']){	//超次
			if($resc['change_time'] >= date('Y')){	//超次且没到下一年
				return $tmp_arr['stat'] = 'err2';	//修改次数已用尽
			}
		}


		if(@$_FILES && $_FILES['banner']){
			$bannerPath = '/banner/'.I('cityCode','p').'/bn_'.$account.'.'.pathinfo($_FILES['banner']['name'],PATHINFO_EXTENSION);
			//上传图片到upyun ,检测 ，缩放组合函数
			$res_upyun = UploadImgToUpyun($this->config,$bannerPath,$_FILES['banner'],1024*200);
			if(is_array($res_upyun)){
				$banner = $bannerPath.'?t='.rand(1,9999);
			}else{
				return $tmp_arr['stat'] = 'err3'; //文件上传失败
			}
		}

		$change_count = $resc['change_count']<$this->config['storeChangeCount']?$resc['change_count']+1:1;

		if($banner){
			$sql = "update {$this->tableName} set stat=1,change_count=?,change_time=?,name=?,phone=?,qq=?,wx=?,addr=?,info=?,banner=? where account = ?";
			$arr = array($change_count,date('Y'),$name,$phone,$qq,$wx,$addr,$info,$banner,$account);
		}else{
			$sql = "update {$this->tableName} set stat=1,change_count=?,change_time=?,name=?,phone=?,qq=?,wx=?,addr=?,info=? where account = ?";
			$arr = array($change_count,date('Y'),$name,$phone,$qq,$wx,$addr,$info,$account);
		}
		$res = $this->dml($sql,$arr);
		if($res){
			$tmp_arr['stat'] = 'ok';
			$tmp_arr['banner'] = $banner;
			return $tmp_arr['banner'];
		}else{
			return $tmp_arr['stat'] = 'err4';	//操作异常
		}
	}




	//商户通过账号修改密码 (p1 原密码)
	public function _updateStorePwd($account,$p1,$p2){
		$sql = "update {$this->tableName} set pwd = ? where account = ? and pwd = ?";
		return $this->dml($sql,array(md5($p2),$account,md5($p1)));
	}



	//通过密保答案找回密码
	public function _findPwd($account,$answer){
		$pwd = rand(111,999);
		$sql = "update {$this->tableName} set pwd = md5($pwd) where account = ? and answer = ?";
		$res = $this->dml($sql,array($account,md5($answer)));
		if($res){
			return $pwd;
		}else{
			return 'err';
		}
	}


	//通过账号查询密保问题
	public function _findQuestion($account){
		$sql = "select question from {$this->tableName} where account = ?";
		return $this->dql($sql,array($account),1);
	}






	//创建商户
	public function _createStore(){
		$keys = '';
		$keysr = '';
		$vals = array();
		$sqlc = "select id from {$this->tableName} where account = ?";
		$resc = $this->dql($sqlc,array(I('account','p')),1);
		if($resc){
			return 'err2';	//账号已存在
		}

		foreach($_POST as $k=>$v){
			if($k == 'db_host' || $k == 'db_name' || $k == 'type'){
				continue;
			}
			if($k == 'cityCode'){
				$keys .= 'city_code,';  //因为数据库中的字段和接收的字段有差异，需要转接
			}else{
				$keys .= $k.',';
			}
			$keysr .= '?,';
			if($k == 'pwd' || $k == 'answer'){
				$vals[] = md5($v);
			}else{
				$vals[] = $v;
			}
		}
		$keys = trim($keys,',');
		$keysr = trim($keysr,',');
		if(@$_FILES['banner']){
			if(!UploadFile_Check($_FILES['banner'],$this->config['imgSize'])){
				return 'err4';	//图片大小超限
			}

			$bannerPath = '/banner/'.I('city_code','p').'/bn_'.I('account','p').'.'.pathinfo($_FILES['banner']['name'],PATHINFO_EXTENSION);
			UpyunAddFile($this->config,$bannerPath,file_get_contents($_FILES['banner']['tmp_name']));
			$keys .= ',banner';
			$keysr .= ',?';
			$vals[] = $bannerPath;
		}
		$sql = "insert into {$this->tableName}({$keys}) values({$keysr}) ";
		$res = $this->dml($sql,$vals);

		if($res){
			return 'ok';
		}else{
			return 'err3';	//创建异常
		}
	}





	//通过账号获取商户信息
	public function _getStoreInfo($account){
		$sql = "select * from {$this->tableName} where account = ? limit 1";
		return $this->dql($sql,array($account),1);
	}




	//通过商户账号更改商户状态
	public function _changeStoreStat($store_account,$store_stat){
		$sql = "update {$this->tableName} set stat = ? where account = ?";
		return $this->dml($sql,array($store_stat,$store_account));
	}




	/*
		### 获取用户列表 (只获取域名项)
		$stat  类别：1待审2正常3未通过4封禁5全部
		$psize 分页条数
		$pnow  当前页
		$search 商户名搜索值
		$feilds 字段类型默认false获取全部字段
					或指定字段如：'a,b,c'
	*/
	public function _getStoreList($stat,$psize,$pnow,$search='',$feilds=false){
		$pnow = intval($pnow)>=1?intval($pnow):1;
		$limit = ($pnow-1) * $psize;
		if(!$feilds){
			$feilds = '*';
		}
		if($stat == 5){	//全部
			if($search){
				$sql = "select {$feilds} from {$this->tableName} use index(name) where name = ? limit 100 union select {$feilds} from {$this->tableName} where name like ? limit {$limit},{$psize}";
				$arr = array($search,"%{$search}%");
			}else{
				$sql = "select {$feilds} from {$this->tableName} order by id desc limit {$limit},{$psize}";
				$arr = array();
			}
		}else{ //按分类
			if($search){	//有搜索值
				$sql = "select {$feilds} from {$this->tableName} use index(name) where stat = {$stat} and name = ? limit 100 union select {$feilds} from {$this->tableName} where stat = {$stat} and name like ? limit {$limit},{$psize}";
				$arr = array($search,"%{$search}%");
			}else{
				$sql = "select {$feilds} from {$this->tableName} where stat = {$stat} order by id desc limit {$limit},{$psize}";
				$arr = array();
			}
		}
		return $this->dql($sql,$arr);
	}

	//获取商户列表全部
	public function _getStoreListAll($cityCode){
		//$sql = "select id,left(name,20),left(info,30),banner from {$this->tableName} where stat = 1 and city_code = ? order by hit";
		$sql = "select id,left(name,5) as name,left(info,15) as info,banner from {$this->tableName} where stat = 2 and city_code = ? order by hit";
		return $this->dql($sql,array($cityCode));
		//return $this->dql($sql,array($cityCode));
	}

}